using System;
using System.Diagnostics;
using NLog;
using NLog.Targets;
namespace VTCeBank.SSO.Utils
{
    public static class LoggerHelper
    {

        /// <summary>
        /// 
        /// </summary>
        volatile static Logger _log;

        private static Logger Log
        {
            get
            {
                if (_log == null)
                {
                    FileTarget target = new FileTarget();
                    target.Layout = "${longdate} ${logger} ${message}";
                    target.FileName = "${basedir}/_LOG/${date:format=yyyyMMdd}_SSO.txt";
                    target.ArchiveFileName = "${basedir}/archives/${date:format=yyyyMMdd}_SSO_log.txt";
                    target.ArchiveAboveSize = 1024 * 1024 * 100; // archive files greater than 10 KB
                   // target.ArchiveNumbering = FileTarget.ArchiveNumberingMode.Sequence;
                    target.Name = "Debug";
                    // this speeds up things when no other processes are writing to the file
                    target.ConcurrentWrites = true;

                    NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Debug);

                    FileTarget warning = new FileTarget();
                    warning.Layout = "${longdate} ${logger} ${message}";
                    warning.FileName = "${basedir}/_LOG/${date:format=yyyyMMdd}_SSO.txt";
                    warning.ArchiveFileName = "${basedir}/archives/${date:format=yyyyMMdd}_SSO_log.txt";
                    warning.ArchiveAboveSize = 1024 * 1024 * 100; // archive files greater than 10 KB
                   // warning.ArchiveNumbering = FileTarget.ArchiveNumberingMode.Sequence;
                    warning.Name = "Warning";
                    // this speeds up things when no other processes are writing to the file
                    warning.ConcurrentWrites = true;
                    NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(warning, LogLevel.Warn);

                    _log = LogManager.GetLogger("paygateSSO");
                }

                return _log;

            }
        }

        /// <summary>
        /// Writes an Error to the log.
        /// </summary>
        /// <param name="ex"></param>
        public static void PublishException(Exception ex)
        {
            Log.Error(":\t" + GetCalleeString() + Environment.NewLine + "\t" + ex.Message + Environment.NewLine + ex.StackTrace);
        }

        /// <summary>
        /// Writes an Error to the log.
        /// </summary>
        /// <param name="ex"></param>
        public static void LogMessage(string message)
        {
            Log.Error(":\t" + GetCalleeString() + Environment.NewLine + "\t" + message);
        }

        /// <summary>
        /// Writes an Error to the log.
        /// </summary>
        /// <param name="ex"></param>
        public static void Warning(string message)
        {
            Log.Warn(":\t" + GetCalleeString() + Environment.NewLine + "\t" + message);
        }

        private static string GetCalleeString()
        {
            foreach (StackFrame sf in new StackTrace().GetFrames())
            {
                if (sf.GetMethod().ReflectedType.Namespace != "VTCeBank.Utility")
                {
                    return string.Format("{0}.{1}", sf.GetMethod().ReflectedType.Name, sf.GetMethod().Name);
                }
            }

            return string.Empty;
        }

    }
}
